package com.boot.web.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

import com.boot.web.dto.LevelDTO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.Level;
import com.boot.web.service.LevelService;

/**
 * 用户等级控制器
 * @author 【zhouJie】
 * @date 2021-06-02 08:43:22
 */
@RestController
@RequestMapping("/level")
public class LevelController {

    @Autowired
    private LevelService levelService;

    @PostMapping("/save")
    public ResponseServer save(LevelDTO dto) {
        Level level = new Level();
        BeanUtils.copyProperties(dto, level);
        if("".equals(dto.getOneProfit())){
            level.setOneProfit(new BigDecimal(0));
        }else{
            level.setOtherProfit(BigDecimal.valueOf(Double.valueOf(dto.getOtherProfit())));
        }
        if("".equals(dto.getTwoProfit())){
            level.setTwoProfit(new BigDecimal(0));
        }else{
            level.setTwoProfit(BigDecimal.valueOf(Double.valueOf(dto.getTwoProfit())));
        }
        if("".equals(dto.getOtherProfit())){
            level.setOtherProfit(new BigDecimal(0));
        }else{
            level.setOtherProfit(BigDecimal.valueOf(Double.valueOf(dto.getOtherProfit())));
        }
        if(!"".equals(dto.getParent())){
            level.setParent(Integer.parseInt(dto.getParent()));
        }
        level.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", levelService.save(level));
    }

    @PutMapping("/update")
    public ResponseServer update(LevelDTO dto) {
        Level level = new Level();
        BeanUtils.copyProperties(dto, level);
        if("".equals(dto.getOneProfit())){
            level.setOneProfit(new BigDecimal(0));
        }else{
            level.setOtherProfit(BigDecimal.valueOf(Double.valueOf(dto.getOtherProfit())));
        }
        if("".equals(dto.getTwoProfit())){
            level.setTwoProfit(new BigDecimal(0));
        }else{
            level.setTwoProfit(BigDecimal.valueOf(Double.valueOf(dto.getTwoProfit())));
        }
        if("".equals(dto.getOtherProfit())){
            level.setOtherProfit(new BigDecimal(0));
        }else{
            level.setOtherProfit(BigDecimal.valueOf(Double.valueOf(dto.getOtherProfit())));
        }
        if(!"".equals(dto.getParent())){
            level.setParent(Integer.parseInt(dto.getParent()));
        }
        level.setUpdateTime(new Date());
        return ResponseServer.success("修改成功", levelService.update(level));
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        return ResponseServer.success("删除成功", levelService.delete(id));
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        return ResponseServer.success("批量删除成功", levelService.batchDelete(ids));
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(levelService.getPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(levelService.getAll());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(levelService.getById(id));
    }

}
